<template>
	<view class="content-img">
		<view class="header-content flex justify-content-space-between" :style="{paddingTop: statusBarHeight}">
			<view style="width: 100rpx;" @click="onTapLeft"><eui-uicons color="#000" type="back" size="40" /></view>
			<view style="font-size: 18px;font-weight: 600;">我的工牌</view>
			<view class="flex justify-content-flex-end" style=" padding-right: 30rpx;width: 100rpx;"
				@click="EditUserData"><eui-uicons color="#000" type="reload" size="40" /></view>
		</view>
	</view>
	<!-- <iframe :src="cUrl" :style="{width:scrollWidth,height:scrollHeight}"
		style="border:0px;-webkit-overflow-scrolling:touch;">
	</iframe> -->
</template>

<script setup type="ts">
	import { onLoad,onReady,onUnload } from '@dcloudio/uni-app';
	import { ref } from 'vue'
	import service from '../../../common/service';
	import { useMainStore } from '../../../store/modules/store';
	import { useLoginStore } from '../../../store/modules/useLoginStore';
	const mainStore = useMainStore();
	const LoginStore = useLoginStore();
	
	const statusBarHeight = ref(uni.getSystemInfoSync().statusBarHeight + 'px');
	const scrollWidth = ref(uni.getSystemInfoSync().windowWidth + 'px');
	const scrollHeight = ref('300px');
	const cTime = ref(0);
	const cInterval = ref(null);
	const cUrl = ref('');
	const triggered = ref(false);
	const cleartime = ref(0);
	
	const iView = plus.webview.create("",'custom-webview', {
			top: uni.getSystemInfoSync().statusBarHeight + 44,
			bottom: uni.getSystemInfoSync().safeAreaInsets.bottom,
			left:0,
			right:0,
		}
	);
	const onTapLeft = () =>{
		uni.navigateBack();
	}
	const EditUserData = () =>{
		getNewUrl();
	}
	const GetSyscfg = ()=>{
		service.enjoyShowLoading('正在加载...');
		createCardView();
		let request = {
		    url: LoginStore.EnjoyMain,
		    data: {
		        UniqueKey: "管易通40公共",
		        ObjectData: {
					whereList:[{
						c_type_name: '',
						c_col: 'c_name',
						c_oper: '=',
						c_value: "管e通员工动态二维码有效期",
						c_col_dtype: "System.String",
						c_logic: "或者",
					}]
				},
		        MethodName: "GetSyscfg",
		    },
		}
		clearInterval(cInterval.value);
		service.request(request, (res) => {
		    if (res && res.data) {
				if (!res.data.HasException) {
					const ReturnObject = res.data.ObjectData.ReturnObject;
					if (ReturnObject != undefined && ReturnObject.length > 0) {
						for (let i = 0; i < ReturnObject.length; i++) {
							if (ReturnObject[i].c_name != undefined && ReturnObject[i].c_name != null && ReturnObject[i].c_name == '管e通员工动态二维码有效期') {
								cTime.value = ReturnObject[i].c_value * 60000;
								cInterval.value = setInterval(function() {
									/* console.log('-------------------------还在执行'); */
									if(cleartime.value==0){
										cUrl.value='';
										getNewUrl();
									}else{
										clearInterval(cInterval.value);
										cInterval.value = null;
									/*console.log('-------------------------走了这里'); */
									}
								}, cTime.value);
							}
						}
					}
					/* const imgUrl = LoginStore.EnjoyMain.replace('/Enjoy/Service', '');
					cUrl.value = imgUrl + '/enjoy/GetUserWorkCardHtml?userNo=' + mainStore.loginInfo.sUser;
					console.log("cUrl.value: " + JSON.stringify(cUrl.value));
					setNewWebview(); */
					getCardHtml();
				} else {
					service.enjoyShowToast(res.data.Exception.Message)
				}
				service.enjoyHideLoading();
		    }
			service.enjoyHideLoading();
		}, () => {
			service.enjoyHideLoading();
		    console.log('失败')
		});
	}
	
	const getCardHtml = () =>{
		let request = {
		    url: LoginStore.EnjoyMain,
		    data: {
		        UniqueKey: "管易通40通讯录",
		        ObjectData: {
					sMkUser: mainStore.loginInfo.sUser
				},
		        MethodName: "GetWorkCardHtml",
		    },
		}
		service.request(request, (res) => {
			console.log(res.data);
		    if (res && res.data) {
				if (!res.data.HasException) {
					cUrl.value = res.data.ObjectData;
					//console.log("cUrl.value: " + JSON.stringify(cUrl.value));
					setNewWebview();
					service.enjoyHideLoading();
				} else {
					service.enjoyShowToast(res.data.Exception.Message)
				}
				service.enjoyHideLoading();
		    }
			service.enjoyHideLoading();
		}, () => {
			service.enjoyHideLoading();
		});
	}
	
	// 下拉刷新
	const onRefresh = ()=>{
		if (triggered.value) {
			return false;
		};
		triggered.value = true;
		setTimeout(() => {
			triggered.value = false;
		}, 1000)
	}
	const getNewUrl = () =>{
		console.log('重新获取')
		service.enjoyShowLoading('正在加载...');
		setTimeout(function() {
			getCardHtml();
		}, 300);
	}
	const setNewWebview = () =>{
		//iView.loadURL(cUrl.value);
		iView.loadData(cUrl.value);
	}
	const createCardView=()=>{
		iView.onloaded=(scuss)=>{
			console.log("scuss: " + JSON.stringify(scuss));
		};
		iView.onerror=(err)=>{
			console.log("err: " + JSON.stringify(err));
		};
		let pages = getCurrentPages();
		let currentPage = pages[pages.length - 1];
		var currentWebview = currentPage.$getAppWebview();
		currentWebview.append(iView);
	}
	onReady(()=>{
		uni.getSystemInfo({
			success: function(res) {
				scrollHeight.value = (res.windowHeight - res.safeAreaInsets.bottom - 70)+'px'
			}
		});
	})
	onLoad(()=>{
		cleartime.value = 0;
		GetSyscfg()
	})
	onUnload(()=>{
		console.log('onUnload')
		cleartime.value = 1;
		clearInterval(cInterval.value);
		cInterval.value = null;
	})
</script>

<style lang="scss" scoped>
	@import '@/main.scss';

	.content-img {
		background-color: #FFFFFF;
		background-repeat: no-repeat;
		width: 100%;
	}

	.flex {
		display: flex;
	}

	.header-content {
		height: 88rpx;
		padding: 0 6px;
		background-color: #fff;
		align-items: center;
	}

	.justify-content-space-between {
		justify-content: space-between !important;
	}

	.justify-content-flex-end {
		justify-content: flex-end;
	}
</style>